package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.applySupplierBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.rejectSupplierBean;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.applySupplierCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.rejectSupplierCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.model.godmch.supplierLog;
import com.ymttest.database.model.godmch.supplierLogExample;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;
import com.ymttest.database.sqlwapper.godmch.supplierLogWapper;
import com.ymttest.utils.TestDataManager;
/**
*供货商审核拒绝
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_rejectSupplier{
	private static rejectSupplierBean bean;
	private static rejectSupplierCall call;
	private static mch_infoWapper mchInfoWapper = new mch_infoWapper();
	private static mch_relationWapper relationWapper=new mch_relationWapper();
	
	
	createIndepMchBean createIndepMchBean=new createIndepMchBean();
	createIndepMchCall createIndepMchCall=new createIndepMchCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("供货商审核拒绝");
	}

	@Before
	public void caseUp() {
		bean=new rejectSupplierBean();
		call=new rejectSupplierCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 校验独立商户 和 供货商
	 * @param mchId
	 * @param supplierId
	 * @throws InterruptedException
	 */
	private void check_mchInfo(Integer mchId,Integer supplierId) throws InterruptedException{
		Logger.comment("验证数据库mch_info表");
		//独立商户
		Map<String, Object> map = new HashMap();
		map.put("mchId", mchId);
		mch_info  mchinfo=mchInfoWapper.selectby(map).get(0);
		//供货商
		Map<String, Object> supplier_map = new HashMap();
		supplier_map.put("mchId", supplierId);
		mch_info  supplier_mchinfo=mchInfoWapper.selectby(supplier_map).get(0);
		Logger.verifyEquals(0, mchinfo.getAgent(), "验证独立商户的Agent");
		Logger.verifyEquals(1, mchinfo.getChannels(), "验证独立商户的channels");
		Logger.verifyEquals(createIndepMchBean.getMchShareType(), mchinfo.getMchShareType(), "验证独立商户的MchShareType");
		Logger.verifyEquals(0, mchinfo.getState(), "验证独立商户的State");
		Logger.verifyEquals(0, supplier_mchinfo.getAgent(), "验证供货商的Agent=0 不变");
		Logger.verifyEquals(1, supplier_mchinfo.getChannels(), "验证供货商的channels");
		Logger.verifyEquals(createIndepMchBean.getMchShareType(), supplier_mchinfo.getMchShareType(), "验证证供货商的MchShareType");
		Logger.verifyEquals(-3, supplier_mchinfo.getState(), "验证证供货商的State=0审核通过");
	}
	/**
	 * 独立商户与供货商关系表
	 * @param mchId
	 * @param supplierId
	 * @throws InterruptedException
	 */
	private void check_relation(Integer mchId,Integer supplierId) throws InterruptedException
	{
		//独立商户与供货商关系表
		Logger.comment("验证数据库mch_relation表");

		//申请供货商关系时，独立商户是代理商，供货商是渠道商身份
		mch_relation relation=relationWapper.selectByPrimaryKey(mchId, supplierId).get(0);
		Logger.verifyEquals(-3, relation.getAgentType(), "验证AgentType");
		Logger.verifyEquals(-3, relation.getAgentModel(), "验证AgentModel");
		Logger.verifyEquals(1, relation.getAddSubAgent(), "验证AddSubAgent");
		Logger.verifyEquals(-3, relation.getState(), "验证state");
	}
	
	/**
	 * 校验供货商日志
	 * @param mchId
	 * @param supplierId
	 * @param supplierLogNum
	 * @throws InterruptedException
	 */
	private void  check_supplierLog(Integer mchId,Integer supplierId,Integer supplierLogNum) throws InterruptedException {
		//供货商日志
		Logger.comment("验证数据库supplierLog表");
		supplierLogExample supplierLogExample=new supplierLogExample();
		supplierLogWapper supplierLogWapper=new supplierLogWapper();
		supplierLogExample.createCriteria().andMchIdEqualTo(mchId).andSupplierIdEqualTo(supplierId).andLogTypeEqualTo(3);
		List<supplierLog> supplierLogs=  supplierLogWapper.selectByExample(supplierLogExample);
		Logger.verifyEquals(supplierLogNum, supplierLogs.size(), "验证supplierLog条数");
		
		Logger.verifyEquals(bean.getReason()==null?"审核拒绝":bean.getReason(), supplierLogs.get(supplierLogs.size()-1).getRemark(), "验证supplierLog表 中的reason");
	}
	/**
	 * 校验
	 * @param mchId
	 * @throws JSONException
	 * @throws InterruptedException
	 */
	private void check_verify(Integer supplierLogNum) throws JSONException, InterruptedException
	{
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(true, call.getMessage().contains("请求成功"), "验证Message");

		
		check_mchInfo(bean.getMchId(), bean.getSupplierId());
		check_relation(bean.getMchId(), bean.getSupplierId()) ;
		check_supplierLog(bean.getMchId(), bean.getSupplierId(),supplierLogNum);
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_001() {
		Logger.start(true,"正例-供货商审核拒绝-mchId是无代理独立商户-成功");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
            bean.setMchId(createIndepMchCall.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
            bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();			
			
			check_verify(1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_002() {
		Logger.start(true,"正例-供货商审核拒绝-mchId是有代理独立商户-成功");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请独立商户的代理
            GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
            bean.setMchId(createIndepMchCall.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
            bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
            
			call.setData(bean);
			call.callService();			
			
			check_verify(1); 
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_004() {
		Logger.start(true,"正例-供货商审核拒绝-商户Id是独立商户的代理-失败");
		try {
			//独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //代理
            applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
            bean.setMchId(applyAgentCall.getAgentId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
            bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
            
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不存在"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_004_1() {
		Logger.start(true,"正例-供货商审核拒绝-mchId是无代理独立商户-供货商状态是被拒绝-失败");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
  
            bean.setMchId(createIndepMchCall.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
            bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
			call.callService();
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不是待审核状态"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_004_2() {
		Logger.start(true,"正例-供货商审核拒绝-mchId是无代理独立商户-供货商状态是被删除-失败");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //审核通过
            bean.setMchId(createIndepMchCall.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
            bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();	
            //删除
            GodmchManageCallService.deleteSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            //再次审核通过
			call.callService();		
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不是待审核状态"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_004_3() {
		Logger.start(true,"正例-供货商审核拒绝-mchId是无代理独立商户-供货商状态是审核通过-失败");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            //审核通过
            GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
            
            bean.setMchId(createIndepMchCall.getmchId());
            bean.setSupplierId(applySupplierCall.getSupplierId());
            bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不是待审核状态"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_005() {
		Logger.start(true,"反例-供货商审核拒绝-mchId不存在-失败");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
			bean.setMchId(createIndepMchCall.getmchId()+999);
	        bean.setSupplierId(applySupplierCall.getSupplierId());
	        bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
	        
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5017", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("上级商户不存在"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_006() {
		Logger.start(true,"反例-供货商审核拒绝-supplierId不存在-失败");
		try {
			//创建独立商户
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
			bean.setMchId(createIndepMchCall.getmchId());
	        bean.setSupplierId(applySupplierCall.getSupplierId()+999);
	        bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
	        
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不存在"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_007() {
		Logger.start(true,"反例-供货商审核拒绝-独立商户与供货商不存在关系-失败");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean1=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall1=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean1);
			//创建独立商户2
			createIndepMchBean createIndepMchBean2=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall2=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean2);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall1.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
			bean.setMchId(createIndepMchCall2.getmchId());
	        bean.setSupplierId(applySupplierCall.getSupplierId());
	        bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
	        
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("5000", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(true, call.getMessage().contains("供货商不存在"), "验证Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_008() {
		Logger.start(true,"反例-供货商审核拒绝-mchId为空-失败");
		try {
			//创建独立商户1
			createIndepMchBean createIndepMchBean1=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall1=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean1);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall1.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
			//bean.setMchId(createIndepMchCall2.getmchId());
	        bean.setSupplierId(applySupplierCall.getSupplierId());
	        bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
	        
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_009() {
		Logger.start(true,"反例-供货商审核拒绝-supplierId为空-失败");
		try {
			//创建独立商户1
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
			bean.setMchId(createIndepMchCall.getmchId());
	        //bean.setSupplierId(applySupplierCall.getSupplierId());
			bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_rejectSupplier_010() {
		Logger.start(true,"反例-供货商审核拒绝-reason为空-数据库中的remark为默认值-成功");
		try {
			//创建独立商户1
			 createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
             createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            //申请供货商
            applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
            applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
            
			bean.setMchId(createIndepMchCall.getmchId());
	        bean.setSupplierId(applySupplierCall.getSupplierId());
			//bean.setReason("自动化测试供货商审核拒绝_"+TestDataManager.getRandomIntNum(5));
			
			call.setData(bean);
			call.callService();			
			
			check_verify(1);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}